package br.kanban.core.busines;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import br.kanban.core.persistence.Historico.TipoHistorico;


@Stateless
public class RelatorioBO implements Serializable{

	private static final long serialVersionUID = 1L;

	@PersistenceContext(unitName="KanbanDS")
	private EntityManager em;
	
	public List<Object[]> relatorioTarefasFinalizada(Date dataInicio, Date dataFim){
		String data = "";
		
		if(dataInicio != null && dataFim != null){
			data = "and h.data between :dataInicio and :dataFim";
		}else if(dataInicio != null){
			data = "and h.data > :dataInicio";			
		}else if(dataFim != null){
			data = "and h.data < :dataFim";
		}
		
		TypedQuery<Object[]> qr =  em.createQuery("select count(h.codigo), h.usuario.nome from Historico h where h.tipo = :tipo " + data +  " group by h.usuario.nome", Object[].class);
		qr.setParameter("tipo", TipoHistorico.FINALIZA);
		
		if(dataInicio != null) qr.setParameter("dataInicio", dataInicio);
		if(dataFim != null) qr.setParameter("dataFim", dataFim);
		
		return qr.getResultList();
		
	}
	
}
